package com._CPA_.controller;
import com._CPA_.pojo.PageBean;
import com._CPA_.pojo.Result;
import com._CPA_.service.RootService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List;
import com._CPA_.pojo.UserInfo;
import com._CPA_.utils.JwtUtils;

import org.slf4j.Logger;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping(value = "/root")
@Api(tags = "用户管理模块")
public class RootController {
    @Autowired
    private RootService rootService;

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param userName
     * @param gender
     * @param ifVip
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/getinfo")
    @ApiOperation(value = "用户列表条件查询" )
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String userName, String gender,Short ifVip,Short ifAdmin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询, 参数: {},{},{},{},{},{},{}",page,pageSize,userName,gender,ifVip,ifAdmin,begin,end);
        //调用service分页查询
        PageBean pageBean = rootService.page(page,pageSize,userName,gender,ifVip,ifAdmin,begin,end);
        System.out.println(pageBean);
        return Result.success(pageBean);
    }

    /**
     * 批量删除功能
     * @param userIds
     * @return
     */
    @DeleteMapping("/del/{userIds}")
    @ApiOperation(value = "批量删除用户" )
    public Result delete(@PathVariable List<String> userIds){
        log.info("批量删除操作, userIds:{}",userIds);
        for (String i:userIds){
            System.out.println(i);
        }
        rootService.delete(userIds);
        return Result.success();
    }


    /**
     * 管理员修改功能
     * @param user
     * @returnHikariCP
     */
    @PutMapping("/edit")
    @ApiOperation(value = "修改用户" )
    public Result rootEdit(@RequestBody UserInfo user){

        log.info("root修改的用户: {}", user);
        //注册判断和生成相应信息
                 if (!rootService.editIfOnlyPhone(user)){
                        return Result.error("手机号重复，请重新输入");
                     }
                 else if (!rootService.editIfOnlyName(user)){
                          return Result.error("用户名重复，请重新输入");
        }
        else {
                     rootService.RootUpdate(user);
                     return Result.success("修改成功");
        }
    }

        /**
         * 管理员添加功能
         * @param user
         * @return
         */
        @PostMapping("/add")
        @ApiOperation(value = "添加用户" )
        public Result rootAdd(@RequestBody UserInfo user)
         {
            log.info("root添加的用户: {}", user);
            if (!rootService.addIfOnlyPhone(user.getPhone())){
                return Result.error("手机号重复，请重新输入");
            }
            else if (!rootService.addIfOnlyName(user.getUserName())){
                return Result.error("用户名重复，请重新输入");
            }
            else {
                rootService.RootAdd(user);
                return Result.success("添加成功");

            }


        }





    /**
     * 管理员登录功能
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "管理员登录" )
    public Result rootLogin(@RequestBody UserInfo user){
        if (user.getUserName()!=null){
            log.info("root用户名登录: {}", user);
            UserInfo u =rootService.rootLoginByName(user);
            //登录成功,生成令牌,下发令牌
            if (u != null){
                if (u.getIfAdmin() == 1){
                    Map<String, Object> c = new HashMap<>();
                    c.put("id", u.getUserId());
                    c.put("name", u.getUserName());
                    c.put("username", u.getPhone());
                    String jwt = JwtUtils.generateJwt(c); //jwt包含了当前登录的员工信息
                    return Result.success(jwt);
                }
            }
            //登录失败, 返回错误信息
            return Result.error("用户名或密码错误");
        }
        else{
            log.info("user手机号登录: {}", user);
            UserInfo u =rootService.rootLoginByPhone(user);
            //登录成功,生成令牌,下发令牌
            if (u != null){
                if (u.getIfAdmin()==1){
                    Map<String, Object> c = new HashMap<>();
                    c.put("id", u.getUserId());
                    c.put("name", u.getUserName());
                    c.put("username", u.getPhone());
                    String jwt = JwtUtils.generateJwt(c); //jwt包含了当前登录的员工信息
                    return Result.success(jwt);
                }
            }
            //登录失败, 返回错误信息
            return Result.error("手机号或密码错误");
        }
    }




}
